
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 05/15/2011 19:56:39
-- Generated from EDMX file: C:\ibupro_bez_TFS\ibupro_TFS\iBUPRO\iBuproModel\iBuproEntities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [C:\IBUPRO_BEZ_TFS\IBUPRO_TFS\IBUPRO\IBUPRO\APP_DATA\DATA\DATABASE.MDF];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserProject_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserProject] DROP CONSTRAINT [FK_UserProject_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserProject_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserProject] DROP CONSTRAINT [FK_UserProject_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectProjectSource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProjectSources] DROP CONSTRAINT [FK_ProjectProjectSource];
GO
IF OBJECT_ID(N'[dbo].[FK_Principal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_Principal];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_ProjectProject];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectOwnerRequest]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OwnerRequests] DROP CONSTRAINT [FK_ProjectOwnerRequest];
GO
IF OBJECT_ID(N'[dbo].[FK_OwnerRequestUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OwnerRequests] DROP CONSTRAINT [FK_OwnerRequestUser];
GO
IF OBJECT_ID(N'[dbo].[FK_DepartmentSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subjects] DROP CONSTRAINT [FK_DepartmentSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_SubjectProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_SubjectProject];
GO
IF OBJECT_ID(N'[dbo].[FK_HistoryBanchelorProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Histories] DROP CONSTRAINT [FK_HistoryBanchelorProject];
GO
IF OBJECT_ID(N'[dbo].[FK_UserBachelorProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_UserBachelorProject];
GO
IF OBJECT_ID(N'[dbo].[FK_UserBachelorProject1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_UserBachelorProject1];
GO
IF OBJECT_ID(N'[dbo].[FK_UserBachelorProject2]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_UserBachelorProject2];
GO
IF OBJECT_ID(N'[dbo].[FK_UserDepartment_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserDepartment] DROP CONSTRAINT [FK_UserDepartment_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserDepartment_Department]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserDepartment] DROP CONSTRAINT [FK_UserDepartment_Department];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRole_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRole] DROP CONSTRAINT [FK_UserRole_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRole_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRole] DROP CONSTRAINT [FK_UserRole_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_UserSubject_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSubject] DROP CONSTRAINT [FK_UserSubject_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserSubject_Subject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserSubject] DROP CONSTRAINT [FK_UserSubject_Subject];
GO
IF OBJECT_ID(N'[dbo].[FK_DiplomProjectProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_DiplomProjectProject];
GO
IF OBJECT_ID(N'[dbo].[FK_DiplomProjectTypeDiplomProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_DiplomProjectTypeDiplomProject];
GO
IF OBJECT_ID(N'[dbo].[FK_StudentPlanSubject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Subjects] DROP CONSTRAINT [FK_StudentPlanSubject];
GO
IF OBJECT_ID(N'[dbo].[FK_StudentPlanStudentField]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StudentFields] DROP CONSTRAINT [FK_StudentPlanStudentField];
GO
IF OBJECT_ID(N'[dbo].[FK_SubjectStudentField_Subject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubjectStudentField] DROP CONSTRAINT [FK_SubjectStudentField_Subject];
GO
IF OBJECT_ID(N'[dbo].[FK_SubjectStudentField_StudentField]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubjectStudentField] DROP CONSTRAINT [FK_SubjectStudentField_StudentField];
GO
IF OBJECT_ID(N'[dbo].[FK_DepartmentUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_DepartmentUser];
GO
IF OBJECT_ID(N'[dbo].[FK_StudentFieldUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_StudentFieldUser];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectTag_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProjectTag] DROP CONSTRAINT [FK_ProjectTag_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectTag_Tag]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProjectTag] DROP CONSTRAINT [FK_ProjectTag_Tag];
GO
IF OBJECT_ID(N'[dbo].[FK_DiplomProjectStateDiplomProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_DiplomProjectStateDiplomProject];
GO
IF OBJECT_ID(N'[dbo].[FK_UserHistory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Histories] DROP CONSTRAINT [FK_UserHistory];
GO
IF OBJECT_ID(N'[dbo].[FK_DepartmentDiplomProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_DepartmentDiplomProject];
GO
IF OBJECT_ID(N'[dbo].[FK_DiplomProject_inherits_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects_DiplomProject] DROP CONSTRAINT [FK_DiplomProject_inherits_Project];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Projects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Projects];
GO
IF OBJECT_ID(N'[dbo].[ProjectSources]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProjectSources];
GO
IF OBJECT_ID(N'[dbo].[OwnerRequests]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OwnerRequests];
GO
IF OBJECT_ID(N'[dbo].[Departments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Departments];
GO
IF OBJECT_ID(N'[dbo].[Subjects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Subjects];
GO
IF OBJECT_ID(N'[dbo].[Histories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Histories];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[DiplomProjectTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DiplomProjectTypes];
GO
IF OBJECT_ID(N'[dbo].[StudentPlans]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StudentPlans];
GO
IF OBJECT_ID(N'[dbo].[StudentFields]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StudentFields];
GO
IF OBJECT_ID(N'[dbo].[Tags]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tags];
GO
IF OBJECT_ID(N'[dbo].[DiplomProjectStates]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DiplomProjectStates];
GO
IF OBJECT_ID(N'[dbo].[Projects_DiplomProject]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Projects_DiplomProject];
GO
IF OBJECT_ID(N'[dbo].[UserProject]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserProject];
GO
IF OBJECT_ID(N'[dbo].[UserDepartment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserDepartment];
GO
IF OBJECT_ID(N'[dbo].[UserRole]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserRole];
GO
IF OBJECT_ID(N'[dbo].[UserSubject]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserSubject];
GO
IF OBJECT_ID(N'[dbo].[SubjectStudentField]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SubjectStudentField];
GO
IF OBJECT_ID(N'[dbo].[ProjectTag]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProjectTag];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FirstName] nvarchar(max)  NOT NULL,
    [LastName] nvarchar(max)  NOT NULL,
    [Password] nvarchar(max)  NOT NULL,
    [Salt] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [Username] nvarchar(max)  NOT NULL,
    [DepartmentAdministratorDepartment_Id] int  NULL,
    [StudentField_Id] int  NULL
);
GO

-- Creating table 'Projects'
CREATE TABLE [dbo].[Projects] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Detail] nvarchar(max)  NOT NULL,
    [Created] datetime  NOT NULL,
    [Shortcut] nvarchar(max)  NOT NULL,
    [Approved] bit  NOT NULL,
    [MemberCount] int  NOT NULL,
    [Locked] bit  NULL,
    [Principal_Id] int  NOT NULL,
    [ParentProject_Id] int  NULL,
    [Subject_Id] int  NULL,
    [DerivedProject_Id] int  NULL
);
GO

-- Creating table 'ProjectSources'
CREATE TABLE [dbo].[ProjectSources] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Url] nvarchar(max)  NOT NULL,
    [Project_Id] int  NOT NULL
);
GO

-- Creating table 'OwnerRequests'
CREATE TABLE [dbo].[OwnerRequests] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [Pending] bit  NOT NULL,
    [Project_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Departments'
CREATE TABLE [dbo].[Departments] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Visible] bit  NOT NULL,
    [Shortcut] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Subjects'
CREATE TABLE [dbo].[Subjects] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Visible] bit  NOT NULL,
    [Shortcut] nvarchar(max)  NOT NULL,
    [Department_Id] int  NULL,
    [StudentPlan_Id] int  NULL
);
GO

-- Creating table 'Histories'
CREATE TABLE [dbo].[Histories] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [Date] datetime  NOT NULL,
    [Readed] bit  NOT NULL,
    [DiplomProject_Id] int  NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'DiplomProjectTypes'
CREATE TABLE [dbo].[DiplomProjectTypes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'StudentPlans'
CREATE TABLE [dbo].[StudentPlans] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Shortcut] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'StudentFields'
CREATE TABLE [dbo].[StudentFields] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Shortcut] nvarchar(max)  NOT NULL,
    [Visible] bit  NOT NULL,
    [StudentPlan_Id] int  NULL
);
GO

-- Creating table 'Tags'
CREATE TABLE [dbo].[Tags] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'DiplomProjectStates'
CREATE TABLE [dbo].[DiplomProjectStates] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Projects_DiplomProject'
CREATE TABLE [dbo].[Projects_DiplomProject] (
    [Literature] nvarchar(max)  NULL,
    [Id] int  NOT NULL,
    [Student_Id] int  NULL,
    [Lector_Id] int  NOT NULL,
    [DepartmentPrincipal_Id] int  NOT NULL,
    [DiplomProjectType_Id] int  NOT NULL,
    [DiplomProjectState_Id] int  NOT NULL,
    [Department_Id] int  NOT NULL
);
GO

-- Creating table 'UserProject'
CREATE TABLE [dbo].[UserProject] (
    [Users_Id] int  NOT NULL,
    [Projects_Id] int  NOT NULL
);
GO

-- Creating table 'UserDepartment'
CREATE TABLE [dbo].[UserDepartment] (
    [DepartmentPrincipal_Id] int  NOT NULL,
    [DepartmentPrincipalDepartments_Id] int  NOT NULL
);
GO

-- Creating table 'UserRole'
CREATE TABLE [dbo].[UserRole] (
    [Users_Id] int  NOT NULL,
    [Roles_Id] int  NOT NULL
);
GO

-- Creating table 'UserSubject'
CREATE TABLE [dbo].[UserSubject] (
    [Lectors_Id] int  NOT NULL,
    [LectorSubjects_Id] int  NOT NULL
);
GO

-- Creating table 'SubjectStudentField'
CREATE TABLE [dbo].[SubjectStudentField] (
    [Subjects_Id] int  NOT NULL,
    [StudentFields_Id] int  NOT NULL
);
GO

-- Creating table 'ProjectTag'
CREATE TABLE [dbo].[ProjectTag] (
    [Projects_Id] int  NOT NULL,
    [Tags_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [PK_Projects]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'ProjectSources'
ALTER TABLE [dbo].[ProjectSources]
ADD CONSTRAINT [PK_ProjectSources]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OwnerRequests'
ALTER TABLE [dbo].[OwnerRequests]
ADD CONSTRAINT [PK_OwnerRequests]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Departments'
ALTER TABLE [dbo].[Departments]
ADD CONSTRAINT [PK_Departments]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Subjects'
ALTER TABLE [dbo].[Subjects]
ADD CONSTRAINT [PK_Subjects]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Histories'
ALTER TABLE [dbo].[Histories]
ADD CONSTRAINT [PK_Histories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'DiplomProjectTypes'
ALTER TABLE [dbo].[DiplomProjectTypes]
ADD CONSTRAINT [PK_DiplomProjectTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StudentPlans'
ALTER TABLE [dbo].[StudentPlans]
ADD CONSTRAINT [PK_StudentPlans]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StudentFields'
ALTER TABLE [dbo].[StudentFields]
ADD CONSTRAINT [PK_StudentFields]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Tags'
ALTER TABLE [dbo].[Tags]
ADD CONSTRAINT [PK_Tags]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'DiplomProjectStates'
ALTER TABLE [dbo].[DiplomProjectStates]
ADD CONSTRAINT [PK_DiplomProjectStates]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [PK_Projects_DiplomProject]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Users_Id], [Projects_Id] in table 'UserProject'
ALTER TABLE [dbo].[UserProject]
ADD CONSTRAINT [PK_UserProject]
    PRIMARY KEY NONCLUSTERED ([Users_Id], [Projects_Id] ASC);
GO

-- Creating primary key on [DepartmentPrincipal_Id], [DepartmentPrincipalDepartments_Id] in table 'UserDepartment'
ALTER TABLE [dbo].[UserDepartment]
ADD CONSTRAINT [PK_UserDepartment]
    PRIMARY KEY NONCLUSTERED ([DepartmentPrincipal_Id], [DepartmentPrincipalDepartments_Id] ASC);
GO

-- Creating primary key on [Users_Id], [Roles_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [PK_UserRole]
    PRIMARY KEY NONCLUSTERED ([Users_Id], [Roles_Id] ASC);
GO

-- Creating primary key on [Lectors_Id], [LectorSubjects_Id] in table 'UserSubject'
ALTER TABLE [dbo].[UserSubject]
ADD CONSTRAINT [PK_UserSubject]
    PRIMARY KEY NONCLUSTERED ([Lectors_Id], [LectorSubjects_Id] ASC);
GO

-- Creating primary key on [Subjects_Id], [StudentFields_Id] in table 'SubjectStudentField'
ALTER TABLE [dbo].[SubjectStudentField]
ADD CONSTRAINT [PK_SubjectStudentField]
    PRIMARY KEY NONCLUSTERED ([Subjects_Id], [StudentFields_Id] ASC);
GO

-- Creating primary key on [Projects_Id], [Tags_Id] in table 'ProjectTag'
ALTER TABLE [dbo].[ProjectTag]
ADD CONSTRAINT [PK_ProjectTag]
    PRIMARY KEY NONCLUSTERED ([Projects_Id], [Tags_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Users_Id] in table 'UserProject'
ALTER TABLE [dbo].[UserProject]
ADD CONSTRAINT [FK_UserProject_User]
    FOREIGN KEY ([Users_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Projects_Id] in table 'UserProject'
ALTER TABLE [dbo].[UserProject]
ADD CONSTRAINT [FK_UserProject_Project]
    FOREIGN KEY ([Projects_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserProject_Project'
CREATE INDEX [IX_FK_UserProject_Project]
ON [dbo].[UserProject]
    ([Projects_Id]);
GO

-- Creating foreign key on [Project_Id] in table 'ProjectSources'
ALTER TABLE [dbo].[ProjectSources]
ADD CONSTRAINT [FK_ProjectProjectSource]
    FOREIGN KEY ([Project_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectProjectSource'
CREATE INDEX [IX_FK_ProjectProjectSource]
ON [dbo].[ProjectSources]
    ([Project_Id]);
GO

-- Creating foreign key on [Principal_Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_Principal]
    FOREIGN KEY ([Principal_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Principal'
CREATE INDEX [IX_FK_Principal]
ON [dbo].[Projects]
    ([Principal_Id]);
GO

-- Creating foreign key on [ParentProject_Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_ProjectProject]
    FOREIGN KEY ([ParentProject_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectProject'
CREATE INDEX [IX_FK_ProjectProject]
ON [dbo].[Projects]
    ([ParentProject_Id]);
GO

-- Creating foreign key on [Project_Id] in table 'OwnerRequests'
ALTER TABLE [dbo].[OwnerRequests]
ADD CONSTRAINT [FK_ProjectOwnerRequest]
    FOREIGN KEY ([Project_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectOwnerRequest'
CREATE INDEX [IX_FK_ProjectOwnerRequest]
ON [dbo].[OwnerRequests]
    ([Project_Id]);
GO

-- Creating foreign key on [User_Id] in table 'OwnerRequests'
ALTER TABLE [dbo].[OwnerRequests]
ADD CONSTRAINT [FK_OwnerRequestUser]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OwnerRequestUser'
CREATE INDEX [IX_FK_OwnerRequestUser]
ON [dbo].[OwnerRequests]
    ([User_Id]);
GO

-- Creating foreign key on [Department_Id] in table 'Subjects'
ALTER TABLE [dbo].[Subjects]
ADD CONSTRAINT [FK_DepartmentSubject]
    FOREIGN KEY ([Department_Id])
    REFERENCES [dbo].[Departments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DepartmentSubject'
CREATE INDEX [IX_FK_DepartmentSubject]
ON [dbo].[Subjects]
    ([Department_Id]);
GO

-- Creating foreign key on [Subject_Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_SubjectProject]
    FOREIGN KEY ([Subject_Id])
    REFERENCES [dbo].[Subjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubjectProject'
CREATE INDEX [IX_FK_SubjectProject]
ON [dbo].[Projects]
    ([Subject_Id]);
GO

-- Creating foreign key on [DiplomProject_Id] in table 'Histories'
ALTER TABLE [dbo].[Histories]
ADD CONSTRAINT [FK_HistoryBanchelorProject]
    FOREIGN KEY ([DiplomProject_Id])
    REFERENCES [dbo].[Projects_DiplomProject]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_HistoryBanchelorProject'
CREATE INDEX [IX_FK_HistoryBanchelorProject]
ON [dbo].[Histories]
    ([DiplomProject_Id]);
GO

-- Creating foreign key on [Student_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_UserBachelorProject]
    FOREIGN KEY ([Student_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserBachelorProject'
CREATE INDEX [IX_FK_UserBachelorProject]
ON [dbo].[Projects_DiplomProject]
    ([Student_Id]);
GO

-- Creating foreign key on [Lector_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_UserBachelorProject1]
    FOREIGN KEY ([Lector_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserBachelorProject1'
CREATE INDEX [IX_FK_UserBachelorProject1]
ON [dbo].[Projects_DiplomProject]
    ([Lector_Id]);
GO

-- Creating foreign key on [DepartmentPrincipal_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_UserBachelorProject2]
    FOREIGN KEY ([DepartmentPrincipal_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserBachelorProject2'
CREATE INDEX [IX_FK_UserBachelorProject2]
ON [dbo].[Projects_DiplomProject]
    ([DepartmentPrincipal_Id]);
GO

-- Creating foreign key on [DepartmentPrincipal_Id] in table 'UserDepartment'
ALTER TABLE [dbo].[UserDepartment]
ADD CONSTRAINT [FK_UserDepartment_User]
    FOREIGN KEY ([DepartmentPrincipal_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [DepartmentPrincipalDepartments_Id] in table 'UserDepartment'
ALTER TABLE [dbo].[UserDepartment]
ADD CONSTRAINT [FK_UserDepartment_Department]
    FOREIGN KEY ([DepartmentPrincipalDepartments_Id])
    REFERENCES [dbo].[Departments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserDepartment_Department'
CREATE INDEX [IX_FK_UserDepartment_Department]
ON [dbo].[UserDepartment]
    ([DepartmentPrincipalDepartments_Id]);
GO

-- Creating foreign key on [Users_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [FK_UserRole_User]
    FOREIGN KEY ([Users_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Roles_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [FK_UserRole_Role]
    FOREIGN KEY ([Roles_Id])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserRole_Role'
CREATE INDEX [IX_FK_UserRole_Role]
ON [dbo].[UserRole]
    ([Roles_Id]);
GO

-- Creating foreign key on [Lectors_Id] in table 'UserSubject'
ALTER TABLE [dbo].[UserSubject]
ADD CONSTRAINT [FK_UserSubject_User]
    FOREIGN KEY ([Lectors_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [LectorSubjects_Id] in table 'UserSubject'
ALTER TABLE [dbo].[UserSubject]
ADD CONSTRAINT [FK_UserSubject_Subject]
    FOREIGN KEY ([LectorSubjects_Id])
    REFERENCES [dbo].[Subjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserSubject_Subject'
CREATE INDEX [IX_FK_UserSubject_Subject]
ON [dbo].[UserSubject]
    ([LectorSubjects_Id]);
GO

-- Creating foreign key on [DerivedProject_Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_DiplomProjectProject]
    FOREIGN KEY ([DerivedProject_Id])
    REFERENCES [dbo].[Projects_DiplomProject]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DiplomProjectProject'
CREATE INDEX [IX_FK_DiplomProjectProject]
ON [dbo].[Projects]
    ([DerivedProject_Id]);
GO

-- Creating foreign key on [DiplomProjectType_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_DiplomProjectTypeDiplomProject]
    FOREIGN KEY ([DiplomProjectType_Id])
    REFERENCES [dbo].[DiplomProjectTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DiplomProjectTypeDiplomProject'
CREATE INDEX [IX_FK_DiplomProjectTypeDiplomProject]
ON [dbo].[Projects_DiplomProject]
    ([DiplomProjectType_Id]);
GO

-- Creating foreign key on [StudentPlan_Id] in table 'Subjects'
ALTER TABLE [dbo].[Subjects]
ADD CONSTRAINT [FK_StudentPlanSubject]
    FOREIGN KEY ([StudentPlan_Id])
    REFERENCES [dbo].[StudentPlans]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentPlanSubject'
CREATE INDEX [IX_FK_StudentPlanSubject]
ON [dbo].[Subjects]
    ([StudentPlan_Id]);
GO

-- Creating foreign key on [StudentPlan_Id] in table 'StudentFields'
ALTER TABLE [dbo].[StudentFields]
ADD CONSTRAINT [FK_StudentPlanStudentField]
    FOREIGN KEY ([StudentPlan_Id])
    REFERENCES [dbo].[StudentPlans]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentPlanStudentField'
CREATE INDEX [IX_FK_StudentPlanStudentField]
ON [dbo].[StudentFields]
    ([StudentPlan_Id]);
GO

-- Creating foreign key on [Subjects_Id] in table 'SubjectStudentField'
ALTER TABLE [dbo].[SubjectStudentField]
ADD CONSTRAINT [FK_SubjectStudentField_Subject]
    FOREIGN KEY ([Subjects_Id])
    REFERENCES [dbo].[Subjects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [StudentFields_Id] in table 'SubjectStudentField'
ALTER TABLE [dbo].[SubjectStudentField]
ADD CONSTRAINT [FK_SubjectStudentField_StudentField]
    FOREIGN KEY ([StudentFields_Id])
    REFERENCES [dbo].[StudentFields]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubjectStudentField_StudentField'
CREATE INDEX [IX_FK_SubjectStudentField_StudentField]
ON [dbo].[SubjectStudentField]
    ([StudentFields_Id]);
GO

-- Creating foreign key on [DepartmentAdministratorDepartment_Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_DepartmentUser]
    FOREIGN KEY ([DepartmentAdministratorDepartment_Id])
    REFERENCES [dbo].[Departments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DepartmentUser'
CREATE INDEX [IX_FK_DepartmentUser]
ON [dbo].[Users]
    ([DepartmentAdministratorDepartment_Id]);
GO

-- Creating foreign key on [StudentField_Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_StudentFieldUser]
    FOREIGN KEY ([StudentField_Id])
    REFERENCES [dbo].[StudentFields]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentFieldUser'
CREATE INDEX [IX_FK_StudentFieldUser]
ON [dbo].[Users]
    ([StudentField_Id]);
GO

-- Creating foreign key on [Projects_Id] in table 'ProjectTag'
ALTER TABLE [dbo].[ProjectTag]
ADD CONSTRAINT [FK_ProjectTag_Project]
    FOREIGN KEY ([Projects_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Tags_Id] in table 'ProjectTag'
ALTER TABLE [dbo].[ProjectTag]
ADD CONSTRAINT [FK_ProjectTag_Tag]
    FOREIGN KEY ([Tags_Id])
    REFERENCES [dbo].[Tags]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectTag_Tag'
CREATE INDEX [IX_FK_ProjectTag_Tag]
ON [dbo].[ProjectTag]
    ([Tags_Id]);
GO

-- Creating foreign key on [DiplomProjectState_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_DiplomProjectStateDiplomProject]
    FOREIGN KEY ([DiplomProjectState_Id])
    REFERENCES [dbo].[DiplomProjectStates]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DiplomProjectStateDiplomProject'
CREATE INDEX [IX_FK_DiplomProjectStateDiplomProject]
ON [dbo].[Projects_DiplomProject]
    ([DiplomProjectState_Id]);
GO

-- Creating foreign key on [User_Id] in table 'Histories'
ALTER TABLE [dbo].[Histories]
ADD CONSTRAINT [FK_UserHistory]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserHistory'
CREATE INDEX [IX_FK_UserHistory]
ON [dbo].[Histories]
    ([User_Id]);
GO

-- Creating foreign key on [Department_Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_DepartmentDiplomProject]
    FOREIGN KEY ([Department_Id])
    REFERENCES [dbo].[Departments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DepartmentDiplomProject'
CREATE INDEX [IX_FK_DepartmentDiplomProject]
ON [dbo].[Projects_DiplomProject]
    ([Department_Id]);
GO

-- Creating foreign key on [Id] in table 'Projects_DiplomProject'
ALTER TABLE [dbo].[Projects_DiplomProject]
ADD CONSTRAINT [FK_DiplomProject_inherits_Project]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------